package com.gd.deron.smartrobot.baobao;


import com.gd.deron.smartrobot.baobao.app.MyApplication;
import com.gd.deron.smartrobot.baobao.recevier.NetwordReceiver;
import com.gd.deron.smartrobot.baobao.service.SerialPostService;
import com.gd.deron.smartrobot.baobao.utils.ActivityUtil;
import com.gd.deron.smartrobot.baobao.utils.Config;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;

public  class BaseActivity extends Activity  {

	ServiceConnection con = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			MyApplication.mPostService = ((SerialPostService.SerialPostBinder) service).getSerialPostService();
		}
		@Override
		public void onServiceDisconnected(ComponentName name) {
			MyApplication.mPostService = null;
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		super.onCreate(savedInstanceState);
		
		ActivityUtil.putActivity(this);
		bindService();
	}
	protected void bindService() {
		boolean bl = bindService(new Intent(BaseActivity.this, SerialPostService.class), con,
					Context.BIND_AUTO_CREATE);
		
		this.registerReceiver(receiver, Config.getfilter());//注册广播接收器
		
	}
	
	@Override
	protected void onStart() {
		super.onStart();
	}
	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0x11:
				String data  = (String) msg.obj;
				receiverData(data);
				break;
			case 0x15:
				wifiConnectSUccessfull();
				break;
			case 0x16:
				wifiConnectFailed();
				break;
			default:
				break;
			}
		};
	};

	private BroadcastReceiver receiver = new BroadcastReceiver(){

		@Override
		public void onReceive(Context context, Intent intent) {
			switch (intent.getAction()) {
			case Config.RECEIVE_DATA:
				String data = intent.getStringExtra("data");
				Message message = Message.obtain();
				message.what = 0x11;
				message.obj = data;
				mHandler.sendMessage(message);
				break;
			case Config.WIFICONNECT_SUCCESSFUL://监听wifi连接状态
				mHandler.sendEmptyMessage(0x15);
				break;
			case Config.WIFICONNECT_FAILED:
				mHandler.sendEmptyMessage(0x16);
				break;
			default:
				break;
			}
		}
		
	};

	public void wifiConnectSUccessfull(){
		//ToastUtils.show(BaseActivity.this, "wifi连接成功");
	}
	protected void receiverData(String data) {
		// TODO Auto-generated method stub
		
	}
	public void wifiConnectFailed(){
		//ToastUtils.show(BaseActivity.this, "wifi连接断开");
	}
	@SuppressWarnings("unused")
	private void startActivity(Class<?> cls) {
        final Intent intent = new Intent(this, cls);
        startActivity(intent);
    }
	@Override
	protected void onResume() {
		super.onResume();
	}
	@Override
	protected void onPause() {
		super.onPause();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(receiver);
	}
	
}
